<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script>
var typeid = '{{.typeid}}';
var product_id = '{{.product_id}}';
var columns = [{"data" : "id", "sWidth":"50px", "bSortable": false, "sTitle":'<input type="checkbox" class="selectAll"/>', render:function (data, type, full, meta) {
        return '<input type="checkbox" class="selectOne" value="'+ full.id +'" />';
    }}];
switch (typeid) {
    case '10':
        columns.push({"data" : "sph", "bSortable": false, "sTitle":"球镜"});
        columns.push({"data" : "cyl", "bSortable": false, "sTitle":"柱镜"});
        $('#search_key').attr('placeholder', '球镜、柱镜');
        break;
    case '20':
        columns.push({"data" : "sph", "bSortable": false, "sTitle":"球镜"});
        columns.push({"data" : "cyl", "bSortable": false, "sTitle":"柱镜"});
        $('#search_key').attr('placeholder', '球镜、柱镜');
        break;
    case '30':
        columns.push({"data" : "model", "bSortable": false, "sTitle":"型号"});
        columns.push({"data" : "color", "bSortable": false, "sTitle":"色号"});
        columns.push({"data" : "size", "bSortable": false, "sTitle":"尺寸"});
        columns.push({"data" : "genre", "bSortable": false, "sTitle":"框型"});
        $('#search_key').attr('placeholder', '型号、色号、尺寸');
        break;
    case '40':
        columns.push({"data" : "sph", "bSortable": false, "sTitle":"球镜"});
        columns.push({"data" : "add", "bSortable": false, "sTitle":"ADD"});
        $('#search_key').attr('placeholder', '球镜、ADD');
        break;
    case '41':
        columns.push({"data" : "model", "bSortable": false, "sTitle":"型号"});
        columns.push({"data" : "color", "bSortable": false, "sTitle":"色号"});
        columns.push({"data" : "size", "bSortable": false, "sTitle":"尺寸"});
        columns.push({"data" : "film_color", "bSortable": false, "sTitle":"膜层颜色"});
        columns.push({"data" : "function_info", "bSortable": false, "sTitle":"功效"});
        columns.push({"data" : "genre", "bSortable": false, "sTitle":"框型"});
        $('#search_key').attr('placeholder', '型号、色号、尺寸');
        break;
    case '70':
        columns.push({"data" : "norms", "bSortable": false, "sTitle":"规格"});
        columns.push({"data" : "place_origin", "bSortable": false, "sTitle":"产地"});
        columns.push({"data" : "dnumber", "bSortable": false, "sTitle":"批准文号"});
        $('#search_key').attr('placeholder', '规格、产地、批准文号');
}
columns.push({
    "mDataProp": "Action",
    "bSortable": false,
    "sWidth":"15%",
    "sTitle":"操作",
    "render": function ( data, type, full, meta ) {
        btns = '<a class="am-icon-edit" href="javascript:;" onclick="actionEdit('+ full.id +');return false;"> 编辑</a>';
        if(full.status == 2) {
            btns += '&nbsp;&nbsp;<a href="javascript:;"  class="am-icon-trash am-text-danger"  onclick="clickStopOrStart('+ full.id +', 1);return false;"> 启用</a>';
        } else {
            btns += '&nbsp;&nbsp;<a href="javascript:;" class="am-icon-trash"  onclick="clickStopOrStart('+ full.id +', 2);return false;"> 停用</a>';
        }
        return btns;
    }
});

var tables = $('#dataTables').DataTable({
    "sAjaxSource" : location.href
    ,"bLengthChange": false
    ,"fnServerData" : function(sSource, aoData, fnCallback) {
        var params = checkQueryTableData(aoData);
        $('#search_key').val() ? params.filter = $('#search_key').val() : '';
        $.post(sSource, JSON.stringify(params),function(resp) {
            fnCallback(resp);
        },'json')
    }
    ,order:[]
    ,columns: columns
    ,"aLengthMenu":[20, 50, 100, 200]
    ,'bLengthChange': true
    ,"serverSide": true
    ,"bPaginate" : true
    ,"bFilter" : false	//是否启动过滤、搜索功能
    ,"bInfo" : true //是否显示页脚信息，DataTables插件左下角显示记录数
    ,"bProcessing" : true //DataTables载入数据时，是否显示‘进度’提示
});

$("#searchsubmit").bind("click", function () { //点击按钮 触发table重新请求服务器
    $("#dataTables").dataTable().fnDraw(false);
});

$('.selectAll').click(function(){
    if($(this).prop('checked') != false) {
        $('.selectOne').prop('checked', true);
    } else {
        $('.selectOne').prop('checked', false);
    }
});

function setStautAjax(poststring) {
    $.post('{{ urlfor "ProductsController.SndStatus" }}', poststring, function (res) {
        if(res.code == 1) {
            layer.alert(res.msg);return false;
        }
        $("#searchsubmit").click();
    }, 'json');
}

//批量停用
$('#batchStop').click(function(){
    var selLine = $('.selectOne[type="checkbox"]:checked');
    if(selLine.length <= 0) {
        layer.alert("请选择需要的行");return false;
    }
    var poststring = 'status=2';
    $.each(selLine, function (index, value) {
        poststring += '&id[]='+$(this).val();
    });
    layer.confirm('是否停用所选参数', function (i) {
        layer.close(i);
        setStautAjax(poststring);
    });
});
//批量启用
$('#batchStart').click(function(){
    var selLine = $('.selectOne[type="checkbox"]:checked');
    if(selLine.length <= 0) {
        layer.alert("请选择需要的行");return false;
    }
    var poststring = 'status=1';
    $.each(selLine, function (index, value) {
        poststring += '&id[]='+$(this).val();
    });
    layer.confirm('是否启用所选参数', function (i) {
        layer.close(i);
        setStautAjax(poststring);
    });
});
function clickStopOrStart(i, s) {
    var poststring = 'status='+ s +'&id[]='+i;
    var tips = '是否停用所选参数';
    if(s == 1) {
        tips = '是否启用所选参数';
    }
    layer.confirm(tips, function (i) {
        layer.close(i);
        setStautAjax(poststring);
    });
}

//新增
function actionInsert() {
    var uri = '{{ urlfor "ProductsController.SndInsert" }}?product_id='+product_id+'&typeid='+typeid;
    var index = layer.open({
        type: 2,
        title:'新增参数',
        shadeClose: false,
        shade: 0.2,
        maxmin: false,
        shift: 1,
        area: ['90%', '80%'],
        content: uri
    });
}
//编辑
function actionEdit(id) {
    var uri = '{{ urlfor "ProductsController.SndUpdate" }}?product_id='+product_id+'&typeid='+typeid+'&id='+id;
    var index = layer.open({
        type: 2,
        title:'编辑参数',
        shadeClose: false,
        shade: 0.2,
        maxmin: false,
        shift: 1,
        area: ['90%', '80%'],
        content: uri
    });
}

function dialogReback() {
    tables.ajax.reload();
}
</script>